extends Piece

signal finish_action

@export var hightlight_sprite:Sprite2D
@export var HP_BAR:NinePatchRect

var move_scope = 2

var move_score_array

var my_attack: Attack

var next_step = null

func _ready():
	max_hp = 5
	left_hp = 5
	is_block = true
	camp = CAMP.ENEMY
	can_move = true
	my_attack = Sweep.new(self)
	my_attack.attack_finish.connect(on_finish_affack)
	hightlight_sprite.visible = false
	HP_BAR.update_hp_bar(max_hp, left_hp)

# 被Controller调用的执行方法
func start_action():
	# 1，执行预存的行动 ------------------------------
	hightlight_sprite.visible = true
	print("hightlight: ",hightlight_sprite.visible)
	await get_tree().create_timer(Global_para.ANIM_STEP_BETWEEN).timeout
	if my_attack.is_set:
		my_attack.do_attack()
	else: 
		on_finish_affack()

func on_finish_affack():
	# 2，计算和执行移动 ------------------------------
	# 初始化移动目标的评分array
	# await get_tree().create_timer(Global_para.ANIM_STEP_BETWEEN).timeout
	move_score_array = []
	for i in range(Chessboard.BOARD_SIZE):
		var new_line = []
		for j in range(Chessboard.BOARD_SIZE):
			new_line.append( - 1)
		move_score_array.append(new_line)
	# 计算各个格子的得分
	gen_move_score(my_coord, move_scope)
	# for i in move_score_array:
	# 	print(i)
	# 获取得分最高值
	var max_move_score = Tool.array_max_2d(move_score_array)
	# print("max: ", max_move_score)
	# 找出所有得分最高值的点
	if max_move_score > - 1: # 如果没有可以移动到的点就不动了
		var max_score_coords = []
		for i in range(len(move_score_array)):
			for j in range(len(move_score_array[i])):
				if move_score_array[i][j] == max_move_score:
					max_score_coords.append([i, j])
		# 移动到随机一个最优点
		var target_coord = max_score_coords[randi() %len(max_score_coords)]
		move(target_coord)
		# 3，计算和存入预存行动 ------------------------------
		var best_attack = my_attack.calculate_max_dmg(target_coord, CAMP.PLAYER)
		# print(best_attack)
		if best_attack[1] != null:
			my_attack.set_attack(best_attack[1], best_attack[2], CAMP.ALL)
			await get_tree().create_timer(Global_para.ANIM_STEP_BETWEEN).timeout
	hightlight_sprite.visible = false
	finish_action.emit()

# 计算可以达到的格子的得分，递归查找所有格子
func gen_move_score(from_coord, left_scope):
	var x = from_coord[0]
	var y = from_coord[1]
	var score = 0
	# 如果目标格已经被占了，则直接设为负数，不可抵达
	if Chessboard.board_array[x][y] != null and Chessboard.board_array[x][y] != self and Chessboard.board_array[x][y].is_block:
		score = -1
	else: # 这个位置没有被占
		var dmg = my_attack.calculate_max_dmg(from_coord, CAMP.PLAYER)[0]
		if dmg > 0: # 如果在这里玩家直接进入攻击范围
			score = dmg * 100
		else: # 如果不行的话，看离玩家还有多远
			for player in Chessboard.player_list:
				var distance = abs(from_coord[0] - player.my_coord[0]) + abs(from_coord[1] - player.my_coord[1])
				score += 10 - distance
			score = score

	move_score_array[x][y] = score
	# 递归查找周围格
	if left_scope > 0:
		if x - 1 >= 0:
			gen_move_score([x - 1, y], left_scope - 1)
		if x + 1 < Chessboard.BOARD_SIZE:
			gen_move_score([x + 1, y], left_scope - 1)
		if y - 1 >= 0:
			gen_move_score([x, y - 1], left_scope - 1)
		if y + 1 < Chessboard.BOARD_SIZE:
			gen_move_score([x, y + 1], left_scope - 1)

func receive_dmg(value_in):
	left_hp -= value_in
	if left_hp<=0:
		before_die()
		queue_free()
	else:
		HP_BAR.update_hp_bar(max_hp, left_hp)

func before_die():
	pass
